# Created by 黄景涛
# DATE: 2025/2/25

import re
from urllib.parse import urljoin

from 蓝鲸各saas登录方案.V5.base_login import bk_login
from 蓝鲸各saas登录方案.V5.config.settings import cfg


# ============================================== smart—saas登录类 =========================================================
class SmartLogin:
    """
    smart_saas登录
    smart_saas，如：itsm、配置管理中心、鲸眼等
    """

    def __init__(self, platform_login_url, username, password, saas_name):
        """
        :param platform_login_url: 平台登录url
        :param username: 用户名
        :param password: 密码
        :param saas_name: smart_saas名
        """
        self.host_prefix = 'apps'
        self.session = bk_login(platform_login_url=platform_login_url, username=username, password=password)
        self.base_host = re.search(r"(http.*\.(?:com|net))", platform_login_url).group(1)
        self.saas_name = saas_name
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                          "Chrome/115.0.0.0 Safari/537.36",
        }

    def login(self) -> dict:
        """ 登录获取cookies字典 ，可用于后续接口的get/post传参cookies """
        home_url = self.get_home_url()
        response = self.session.get(home_url, headers=self.headers, verify=False)
        assert response.status_code in (200, 201), f'{home_url}访问失败，状态码：{response.status_code}'
        # print(response.text)
        return self.session.cookies.get_dict()

    def get_bk_token(self) -> str:
        """ 获取蓝鲸平台的bk_token """
        return self.session.cookies.get('bk_token')

    def get_home_url(self) -> str:
        """ 构造saas的首页接口 """
        smart_host = re.sub(r'://', f'://{self.host_prefix}.', self.base_host)
        home_url = urljoin(smart_host, f'{self.saas_name}/')
        print(f'smart-saas首页地址：{home_url}')
        return home_url


# ============================================== 内置saas登录类 =========================================================

class InnerLogin(SmartLogin):
    """
    内置saas登录
    内置saas，如：cmdb_platform、权限中心、用户管理等
    """

    def __init__(self, platform_login_url, username, password, saas_name):
        """
        :param platform_login_url: 平台登录url
        :param username: 用户名
        :param password: 密码
        :param saas_name: smart_saas名
        """
        super().__init__(platform_login_url, username, password, saas_name)
        self.host_prefix = saas_name

    def login(self) -> dict:
        """ 登录获取cookies字典 ，可用于后续接口的get/post传参cookies """
        return super().login()

    def get_bk_token(self) -> str:
        """ 获取蓝鲸平台的bk_token """
        return super().get_bk_token()

    def get_home_url(self) -> str:
        """ 构造saas的首页接口 """
        smart_host = re.sub(r'://', f'://{self.host_prefix}.', self.base_host)
        print(f'内置saas首页地址：{smart_host}')
        return smart_host


if __name__ == '__main__':
    login_url = cfg.get('login_url')
    username = cfg.get('username')
    password = cfg.get('password')
    saas_name = cfg.get('smart_saas')
    print(login_url)

    lg = SmartLogin(login_url, username, password, saas_name)
    print(lg.login())

    saas_name = cfg.get('inner_saas')
    lg = InnerLogin(login_url, username, password, saas_name)
    print(lg.login())
